package com.ywjt.pinkelephant.common;

import android.content.Context;
import android.content.SharedPreferences;

import com.ywjt.pinkelephant.MyApp;


public class UserData {
    private Context context;
    private static UserData UD = null;//会员业务逻辑类 对象
    public static final String USER_FILE_NAME = "user_info";//会员信息文件名


    public static final String ACCOUNT="account";//账号
    public static final String ALLBALANCE="allBalance";//总收益
    public static final String alipayAccount="alipayAccount";//支付宝账户
    public static final String BALANCE="balance";//余额
    public static final String GOLDINTEGRAL="goldIntegral";//金积分
    public static final String SILVERINTEGRAL="silverIntegral";//银积分
    public static final String AVATAR = "avatar";//头像地址
    public static final String MERCHANTID="merchantId";//商户id
    public static final String UID = "id";//用户id
    public static final String USERNAME="username";
    public static final String NICKNAME="nickname";
    public static final String GOLDCOIN="goldCoin";
    public static final String WXPAY="wxPay";//微信支付
    public static final String ZFBPAY="zfbPay";//支付宝
    public static final String ISVIP="isVip";//是否是VIP  0 不是 1 是
    public static final String startTime="startTime";//会员开始时间
    public static final String endTime="endTime";//会员结束时间




    private UserData(Context context) {
        this.context = context;
    }

    /**
     * 单利模式
     *
     * @param context
     * @return
     */
    public static synchronized UserData create(Context context) {
        return create();
    }

    /**
     * 单利模式
     *
     * @return
     */
    public static synchronized UserData create() {
        if (UD == null)
            UD = new UserData(MyApp.context);
        return UD;
    }

    /**
     * 改变登陆状态  清空保存数据
     *
     * @param islogin
     * @return
     */
    public UserData ChangeLoginState(boolean islogin) {
        //退出登陆
        if (!islogin) {
            clearAll();
        }
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = sp.edit();
        ed.commit();
        return UD;
    }

    private void clearAll() {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = sp.edit();
        ed.clear();
        ed.commit();
    }

    /**
     * 获取数据(String类型)
     *
     * @param key
     * @return
     */
    public String get(String key) {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        return sp.getString(key, null);
    }

    /**
     * 获取数据(int类型)
     *
     * @param key
     * @return
     */
    public int get(String key, int defValue) {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        return sp.getInt(key, defValue);
    }

    /**
     * 获取数据(long类型)
     *
     * @param key
     * @return
     */
    public long getL(String key, long defValue) {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        return sp.getLong(key, defValue);
    }

//    /**
//     * 保存数据
//     *
//     * @param userBean
//     * @return
//     */
//    public boolean saveUserData(LoginModel.ResultDTO.UserMessageDTO userBean) {
//        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
//        SharedPreferences.Editor ed = sp.edit();
//
//        try {
//            ed.putString(NICKNAME,userBean.getNickname());
//        }catch (Exception e){
//            ed.putString(NICKNAME, "");
//        }
//
//        try {
//            ed.putString(MERCHANTID,userBean.getMerchantId());
//        }catch (Exception e){
//            ed.putString(MERCHANTID, "");
//        }
//
//        try {
//            ed.putString(AVATAR, userBean.getAvatar());
//        } catch (Exception e) {
//            ed.putString(AVATAR, "");
//        }
//
//
//        try {
//            ed.putString(GOLDINTEGRAL, userBean.getGoldIntegral()+"");
//        } catch (Exception e) {
//            ed.putString(GOLDINTEGRAL, "");
//        }
//        try {
//            ed.putString(SILVERINTEGRAL, userBean.getSilverIntegral()+"");
//        } catch (Exception e) {
//            ed.putString(SILVERINTEGRAL, "");
//        }
//
//
//        try {
//            ed.putString(BALANCE, userBean.getBalance()+"");
//        } catch (Exception e) {
//            ed.putString(BALANCE, "");
//        }
//
//        try {
//            ed.putString(UID, userBean.getId());
//        } catch (Exception e) {
//            ed.putString(UID, "");
//        }
//
//
//        return ed.commit();
//    }

    /**
     * 改变数据
     *
     * @param key
     * @param value
     * @return
     */
    public boolean changeUserData(String key, String value) {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = sp.edit();

        try {
            ed.putString(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ed.commit();
    }

    /**
     * 改变数据
     *
     * @param key
     * @param value
     * @return
     */
    public boolean changeUserData(String key, int value) {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = sp.edit();

        try {
            ed.putInt(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ed.commit();
    }

    /**
     * 改变数据
     *
     * @param key
     * @param value
     * @return
     */
    public boolean changeUserData(String key, long value) {
        SharedPreferences sp = context.getSharedPreferences(USER_FILE_NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor ed = sp.edit();

        try {
            ed.putLong(key, value);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return ed.commit();
    }
}
